package com.shenchuangit.rydemo.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;


public class WordUtils {

    public static void xmlTest(OutputStream bos, InputStream is, Map<String,Object> param) {
        ZipInputStream zin = new ZipInputStream(is);
        ZipOutputStream zos = new ZipOutputStream(bos);
        try {
            ZipEntry entry;
            while((entry = zin.getNextEntry()) != null) {
                //把输入流的文件传到输出流中 如果是word/document.xml由我们输入
                zos.putNextEntry(new ZipEntry(entry.getName()));
                if("word/document.xml".equals(entry.getName())){
                    String xml = new BufferedReader(new InputStreamReader(zin)).lines().collect(Collectors.joining(System.lineSeparator()));
                    //先做字符串的解析测试吧
                    //需要取的字符串:1.<w:body></w:body>
                    //             2.<wx:sect></wx:sect>
                    //              3.<w:tr></w:tr>
                    String a = regex(xml,"w:body","");
                    //body中的内容
                    //复制测试
                    String b = "<wx:sect>"+a+"</wx:sect>";
                    b = b + b;
                    replace1(xml,a,b);
                }else {
                    int c;
                    while ((c = zin.read()) != -1) {
                        zos.write(c);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                zos.close();
                zin.closeEntry();
                zin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public  static String regex(String xml,String label,String field) {
        String context = "";
        //正则表达式
        String rgex = "<"+label+">(.*?)</"+label+">";
        Pattern pattern = Pattern.compile(rgex);// 匹配的模式
        Matcher m = pattern.matcher(xml);
        //匹配的有多个
        List<String> list = new ArrayList<String>();
        while (m.find()) {
            int i = 1;
            list.add(m.group(i));
            i++;
        }
        if("".equals(field)) {
            //只要匹配的第一个
            if(list.size()>0){
                context = list.get(0);
            }
        }else{
            //匹配包含field的
            if(list.size()>0) {
                for(int i = 0 ; i<list.size(); i++) {
                    if(list.get(i).indexOf(field)!=-1){
                        context = list.get(i);
                    }
                }
            }
        }

        return context;
    }
    public   static   String   replace1(String   s,String   org,String   ob)
    {
        String   newString="";
        int   first=0;
        while   (s.indexOf(org)!=-1)
        {
            first=s.indexOf(org);
            if   (first!=s.length()   )
            {
                newString=newString+s.substring(0,first)+ob   ;
                s=s.substring(first+org.length()   ,s.length()   )   ;
            }

        }

        newString=newString+s;
        return   newString;
    }
    public static void editDocx(OutputStream bos, InputStream is, Map<String,Object> param){
        ZipInputStream zin = new ZipInputStream(is);
        ZipOutputStream zos = new ZipOutputStream(bos);
        try {
            ZipEntry entry;
            while((entry = zin.getNextEntry()) != null) {
                //把输入流的文件传到输出流中 如果是word/document.xml由我们输入
                zos.putNextEntry(new ZipEntry(entry.getName()));
                if("word/document.xml".equals(entry.getName())){
                    String xml = new BufferedReader(new InputStreamReader(zin)).lines().collect(Collectors.joining(System.lineSeparator()));
                    //取出表格内容
                    String tableContent = "<w:tr>"+regex(xml,"w:tr","{f4}")+"</w:tr>";
                    String newTableContent = "";
                            //复制多行
                    List<List<String>> tableList = (List<List<String>>) param.get("tableList");
                    for(int i = 0 ; i<tableList.size(); i++) {
                        List<String> tableContentList = tableList.get(i);
                        String newTableCell = tableContent;
                        for(int i2 = 0 ; i2 < tableContentList.size(); i2++) {
                            String no = (4 + i2) + "";
                            newTableCell = newTableCell.replace("{f"+no+"}",tableContentList.get(i2));
                        }
                        newTableContent = newTableContent + newTableCell;
                    }

                    xml = replace1(xml,tableContent,newTableContent);
                    //整段内容
                    String a = regex(xml,"w:body","");
                    //body中的内容
                    String b = a + a;
                    String xml1 = replace1(xml,a,b);
                    for (Entry<String, Object> e : param.entrySet()) {
                        if (xml1.contains(e.getKey()))
                            xml1 = xml1.replace(e.getKey(),(String)e.getValue());
                    }
                    ByteArrayInputStream byteIn = new ByteArrayInputStream(xml1.getBytes());
                    int c;
                    while ((c = byteIn.read()) != -1) {
                        zos.write(c);
                    }
                    byteIn.close();
                }else {
                    int c;
                    while ((c = zin.read()) != -1) {
                        zos.write(c);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                zos.close();
                zin.closeEntry();
                zin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}